<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="物料编码" prop="mateCode">-->
<!--      <el-input v-model="dataForm.mateCode" placeholder="物料编码"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="物料名称" prop="mateName">
      <el-input v-model="dataForm.mateName" placeholder="物料名称"></el-input>
    </el-form-item>
    <el-form-item label="物料单位" prop="company">
      <el-input v-model="dataForm.company" placeholder="物料单位"></el-input>
    </el-form-item>
    <el-form-item label="标准库存" prop="standInventory">
      <el-input v-model="dataForm.standInventory" placeholder="标准库存"></el-input>
    </el-form-item>
    <el-form-item label="当前库存" prop="currentInventory">
      <el-input v-model="dataForm.currentInventory" placeholder="当前库存"></el-input>
    </el-form-item>
    <el-form-item label="采购价格" prop="purPrice">
      <el-input v-model="dataForm.purPrice" placeholder="采购价格"></el-input>
    </el-form-item>
    <el-form-item label="销售价格" prop="selPrice">
      <el-input v-model="dataForm.selPrice" placeholder="销售价格"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          mateCode: '',
          mateName: '',
          company: '',
          standInventory: '',
          currentInventory: '',
          purPrice: '',
          selPrice: ''
        },
        dataRule: {
          // mateCode: [
          //   { required: true, message: '物料编码不能为空', trigger: 'blur' }
          // ],
          mateName: [
            { required: true, message: '物料名称不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '物料单位不能为空', trigger: 'blur' }
          ],
          standInventory: [
            { required: true, message: '标准库存不能为空', trigger: 'blur' }
          ],
          currentInventory: [
            { required: true, message: '当前库存不能为空', trigger: 'blur' }
          ],
          purPrice: [
            { required: true, message: '采购价格不能为空', trigger: 'blur' }
          ],
          selPrice: [
            { required: true, message: '销售价格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              // url: this.$http.adornUrl(`/rmg/matter/info/${this.dataForm.id}`),
              url: `http://localhost:8081/rmg/matter/info/${this.dataForm.id}`,
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              alert(data)
              if (data && data.code === 0) {
                this.dataForm.mateCode = data.matter.mateCode
                this.dataForm.mateName = data.matter.mateName
                this.dataForm.company = data.matter.company
                this.dataForm.standInventory = data.matter.standInventory
                this.dataForm.currentInventory = data.matter.currentInventory
                this.dataForm.purPrice = data.matter.purPrice
                this.dataForm.selPrice = data.matter.selPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              // url: this.$http.adornUrl(`/rmg/matter/${!this.dataForm.id ? 'save' : 'update'}`),
              url: `http://localhost:8081/rmg/matter/${!this.dataForm.id ? 'save' : 'update'}`,

              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'mateCode': this.dataForm.mateCode,
                'mateName': this.dataForm.mateName,
                'company': this.dataForm.company,
                'standInventory': this.dataForm.standInventory,
                'currentInventory': this.dataForm.currentInventory,
                'purPrice': this.dataForm.purPrice,
                'selPrice': this.dataForm.selPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
